package com.taotao.sso.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbUserMapper;
import com.taotao.pojo.TbUser;
import com.taotao.pojo.TbUserExample;
import com.taotao.pojo.TbUserExample.Criteria;
import com.taotao.sso.jedis.JedisClient;
import com.taotao.sso.service.UserService;
import com.taotao.utils.JsonUtils;


@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private TbUserMapper tbMapper;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${USER_INFO}")
	private String USER_INFO;
	
	@Value("${SESSION_EXPIRE}")
	private Integer SESSION_EXPIRE;

	@Override
	public TaotaoResult checkData(String param, int type) {
		TbUserExample example=new TbUserExample();
		Criteria criteria=example.createCriteria();
		
		if(type==1){
			criteria.andUsernameEqualTo(param);
		}else if(type==2){
			criteria.andPhoneEqualTo(param);
		}else if(type==3){
			criteria.andEmailEqualTo(param);
		}else{
			return TaotaoResult.build(400, "erro param");
		}
		
		List<TbUser> list=tbMapper.selectByExample(example);
		
		if(list==null||list.size()==0){
			return TaotaoResult.ok(true);
		}
		
		
		
		return TaotaoResult.ok(false);
	}

	@Override
	public TaotaoResult createUser(TbUser user) {

		if(StringUtils.isBlank((user.getUsername()))){
			return TaotaoResult.build(400, "username not exits");
			
		}
		if(StringUtils.isBlank((user.getPassword()))){
			return TaotaoResult.build(400, "username not exits");
			
		}
		TaotaoResult result=checkData(user.getUsername(), 1);
		if(!(boolean) result.getData()){
			return TaotaoResult.build(400, "username already exits");
		}
		
		if (StringUtils.isNotBlank(user.getPhone())) {
			result = checkData(user.getPhone(), 2);
			if (!(boolean) result.getData()) {
				return TaotaoResult.build(400, "phonenum is used");
			}
		}
		if (StringUtils.isNotBlank(user.getEmail())) {
			result = checkData(user.getEmail(), 3);
			if (!(boolean) result.getData()) {
				return TaotaoResult.build(400, "Email is used");
			}
		}
		
		user.setCreated(new Date());
		user.setUpdated(new Date());
		String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
		user.setPassword(md5Pass);
		tbMapper.insert(user);
		return TaotaoResult.ok();

	}

	@Override
	public TaotaoResult login(String username, String password) {
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<TbUser> list = tbMapper.selectByExample(example);
		if (list == null || list.size() == 0) {
			return TaotaoResult.build(400, "username or password error");
		}
		TbUser user = list.get(0);
		if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
			return TaotaoResult.build(400, "username or password error");
		}
		String token = UUID.randomUUID().toString();
		user.setPassword(null);
		jedisClient.set(USER_INFO + ":" + token, JsonUtils.objectToJson(user));
		jedisClient.expire(USER_INFO + ":" + token, SESSION_EXPIRE);
		return TaotaoResult.ok(token);
	}


	@Override
	public TaotaoResult getUserByToken(String token) {
		String json = jedisClient.get(USER_INFO + ":" + token);
		if (StringUtils.isBlank(json)) {
			return TaotaoResult.build(400, "please login again");
		}
		jedisClient.expire(USER_INFO + ":" + token, SESSION_EXPIRE);
		TbUser user = JsonUtils.jsonToPojo(json, TbUser.class);
		return TaotaoResult.ok(user);
	}

}
